<script>
	import chat from '@/libs/chat.js'
	import {
		getAppVersion
	} from '@/api/common.js'
	import {
		getMessageDetail,
		checkInfo,
	} from '@/api/message.js'
	import {
		useLocale
	} from '@/locale/useLocale';
	import {
		useChatStore
	} from '@/store/modules/chat'
	import {
		useUserStore
	} from '@/store/modules/user'
	const chatStore = useChatStore()

	export default {
		data() {
			return {
				version: 0,
				resVersion: 0,
				modileSystem: 'android',
				Apk: ''
			}
		},
		onLaunch() {
			// #ifdef APP-PLUS
			/* uniPush */
			// uni.getPushClientId({
			// 	success: (res) => {
			// 		userStore.setCid(res.cid)
			// 		this.handlePush()
			// 	},
			// 	fail(err) {}
			// })
			/* H5+ */
			/* 获取设备信息 */
			uni.getSystemInfo({
				success(res) {
					uni.setStorageSync('systemInfo', res.ua)
				}
			})
			// this.handlePush()
			// this.getAppVersion()
			// #endif
			const token = uni.getStorageSync("token");
			// if (!token) return
			// chat.initSocket()
			// const userStore = useUserStore()
			// userStore.getCurrentUser().then(res => {
			// 	const {
			// 		initLocale
			// 	} = useLocale();
			// 	initLocale()
			// })
		},
		methods: {
			getAppVersion() {
				getAppVersion().then(res => {
					let data = res.data.sysVersion || ''
					const matches = data.match(/(\d+\.\d+\.\d+)/);
					if (matches && matches.length > 0) this.Apk =
						`https://cdn.jnpfsoft.com/apk/Android-java-${matches[0]}.apk`
					data.trim();
					this.version = Number(data.replace(/[^0-9]/ig, ""))
					this.$nextTick(() => {
						this.onUpdate()
					})
				}).catch((err) => {})
			},
			onUpdate() {
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					let resVersion = this.define.sysVersion;
					resVersion.trim();
					this.resVersion = Number(resVersion.replace(/[^0-9]/ig, ""))
					if (this.version != this.resVersion) {
						process.env.NODE_ENV === "production" ?
							uni.setStorageSync('isUpdate', 1) : uni.removeStorageSync('isUpdate')
						uni.showModal({ //提醒用户更新
							title: "立即更新版本",
							success: (res) => {
								if (res.confirm) {
									uni.removeStorageSync('isUpdate')
									let system = plus.os.name;
									if (system === 'Android') {
										// let url = devLanguage ? javaApk : dotNetApk;
										if (!this.Apk) return this.$u.toast('下载链接为空')
										plus.runtime.openURL(this.Apk)
										// uni.downloadFile({
										// 	//下载地址
										// 	url: url,
										// 	success: data => {
										// 		if (data.statusCode === 200) {
										// 			plus.runtime.install(data
										// 				.tempFilePath, {
										// 					force: false
										// 				},
										// 				function() {
										// 					plus.runtime
										// 						.restart();
										// 				});
										// 		}
										// 	}
										// })
									} else {
										plus.runtime.launchApplication({
											action: `itms-apps://itunes.apple.com/cn/app/id${'appleId自行配置'}`
										}, function(e) {});
									}
								} else if (res.cancel) {
									if (this.modileSystem == 'ios') {
										plus.ios.import("UIApplication")
											.sharedApplication()
											.performSelector("exit")
									} else if (this.modileSystem == 'android') {
										plus.runtime.quit();
									}
								}
							}
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/vk-uview-ui/index.scss";
	@import "@/assets/iconfont/ym/iconfont.css";
	@import "@/assets/iconfont/custom/iconfont.css";
	@import "@/assets/scss/common.scss";
	@import "@/assets/scss/components.scss";
	.price {
		font-size: 28rpx;
		color: #FF6B6B;
		font-weight: 600;
	}
</style>